<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/12/6 0006
  Time: 16:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>cookie记录访问次数</title>
</head>
<body>
<%
     //1：拿到所有的cookie
    Cookie cr[]=request.getCookies();
     //2:  判断有没有记录次数的那个cookie
     //有，+1,没有，新建一个
    //visitnum
    int visitnum=0;
    for(int i=0;cr!=null&&i<cr.length;i++){
        if(cr[i].getName().equals("visitnum")){
            visitnum=Integer.parseInt(cr[i].getValue());
        }
    }
    visitnum++;
    Cookie c=new Cookie("visitnum",String.valueOf(visitnum));
    c.setMaxAge(60*60*24*7);
    response.addCookie(c);
%>
<h1 style="color: red;font-size:99px">您已经访问了<%=visitnum%>次</h1>
</body>
</html>
